<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admins
 *
 * @author Maulnick
 */
class User_Model extends CI_Model {
    
    //***************/
    //  RETRIEVAL
    //***************/
    
    function get_validation()
    {
        $user = array();
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('user');
        if($query->num_rows==1)
        {
            $user['user_id'] = $query->row(0)->user_id;
            $user['username'] = $query->row(0)->username;
            $user['role_id'] = $query->row(0)->role_id;
            return $user;
        }else{
            return false;
        }
		
    }
    function get_last_user_index($idrole){
        $table = $this->get_table_by_role($idrole);
        //get last index article
        $this->db->select($table.'.'.$table.'_id');
        $this->db->from($table);
        $this->db->order_by($table.'.'.$table.'_id', "desc");
        $this->db->limit(1);
        $result = $this->db->get();
        $id = $table.'_id';
        if($result->num_rows==1){
            return $result->row(0)->$id;
        }else{
            return false;
        }
    }
    function get_user($role_id, $user_id){
        $table = $this->get_table_by_role($role_id);
        //get user info using idrole and iduser
        $this->db->where($table.'.'.$table.'_id',$user_id);
        $result = $this->db->get($table);
        if($result->num_rows==1){
            if($table=='consignee'){
                $result->row(0)->role = 'consignee (on behalf forwarder)';
            }else{
                $result->row(0)->role = $table;
            }
            return $result->row(0);
        }else{
            return false;
        }
    }
    
    function check_username($username){
        $this->db->where('username',$username);
        $result = $this->db->get('user');
        if($result->num_rows==1){
            return true;
        }else{
            return false;
        }
    }
    
    function get_all_consignee(){
        $result = $this->db->get('consignee');
        if($result->num_rows>0){
            return $result->result();
        }else{
            return false;
        }
    }
    function get_all_shippingline(){
        $result = $this->db->get('shippingline');
        if($result->num_rows>0){
            return $result->result();
        }else{
            return false;
        }
    }
    function get_all_insw(){
        $result = $this->db->get('insw');
        if($result->num_rows>0){
            return $result->result();
        }else{
            return false;
        }
    }
    function get_username_by_role_and_id($role_id, $user_id){
        $result = false;
        if($role_id==ROLE_CONSIGNEE){
            $result = $this->get_consignee_username_by_id($user_id);
        }else if($role_id==ROLE_SHIPPINGLINE){
            $result = $this->get_shippingline_username_by_id($user_id);
        }else if($role_id==ROLE_OPERATORTERMINAL){
            $result = $this->get_operatorterminal_username_by_id($user_id);
        }else if($role_id==ROLE_TRUCKING){
            $result = $this->get_trucking_username_by_id($user_id);
        }else if($role_id==ROLE_INSW){
            $result = $this->get_insw_username_by_id($user_id);
        }
        return $result;
    }
    function get_consignee_username_by_id($id){
        //get username consignee info using idrole and iduser
        $this->db->select('user.username');
        $this->db->where('consignee.consignee_id',$id);
        $this->db->join('user', 'user.user_id = consignee.consignee_id and user.role_id = '.ROLE_CONSIGNEE);
        $this->db->limit(1);
        $result = $this->db->get('consignee');
        if($result->num_rows==1){
            return $result->row(0)->username;
        }else{
            return false;
        }
    }
    function get_shippingline_username_by_id($id){
        //get username shippingline info using idrole and iduser
        $this->db->select('user.username');
        $this->db->where('shippingline.shippingline_id',$id);
        $this->db->join('user', 'user.user_id = shippingline.shippingline_id and user.role_id = '.ROLE_SHIPPINGLINE);
        $this->db->limit(1);
        $result = $this->db->get('shippingline');
        if($result->num_rows==1){
            return $result->row(0)->username;
        }else{
            return false;
        }
    }
    
    function get_operatorterminal_username_by_id($id){
        //get username operatorterminal info using idrole and iduser
        $this->db->select('user.username');
        $this->db->where('operatorterminal.operatorterminal_id',$id);
        $this->db->join('user', 'user.user_id = operatorterminal.operatorterminal_id and user.role_id = '.ROLE_OPERATORTERMINAL);
        $this->db->limit(1);
        $result = $this->db->get('operatorterminal');
        if($result->num_rows==1){
            return $result->row(0)->username;
        }else{
            return false;
        }
    }
    function get_trucking_username_by_id($id){
        //get username trucking info using idrole and iduser
        $this->db->select('user.username');
        $this->db->where('trucking.trucking_id',$id);
        $this->db->join('user', 'user.user_id = trucking.trucking_id and user.role_id = '.ROLE_TRUCKING);
        $this->db->limit(1);
        $result = $this->db->get('trucking');
        if($result->num_rows==1){
            return $result->row(0)->username;
        }else{
            return false;
        }
    }
    function get_insw_username_by_id($id){
        //get username shippingline info using idrole and iduser
        $this->db->select('user.username');
        $this->db->where('insw.insw_id',$id);
        $this->db->join('user', 'user.user_id = insw.insw_id and user.role_id = '.ROLE_INSW);
        $this->db->limit(1);
        $result = $this->db->get('insw');
        if($result->num_rows==1){
            return $result->row(0)->username;
        }else{
            return false;
        }
    }
    //***************/
    //  INSERTION
    //***************/
    
    function insert_registration($user, $admin){
        // insert data to usertabel ex consignee, shippinglines
        $tableuser = $this->get_table_by_role($admin['role_id']);
        $query = $this->db->insert($tableuser, $user);
        // get last index of usertabel ex consignee, shippinglines
        $admin['user_id'] = $this->get_last_user_index($admin['role_id']);
        // insert data to admin for username and password
        $query2 = $this->db->insert('user', $admin);
        return ($query && $query2);
    }
    
    private function get_table_by_role($idrole){
        if($idrole==ROLE_CONSIGNEE){
            $tableuser = 'consignee';
        }else if($idrole==ROLE_SHIPPINGLINE){
            $tableuser = 'shippingline';
        }else if($idrole==ROLE_OPERATORTERMINAL){
            $tableuser = 'operatorterminal';
        }else if($idrole==ROLE_TRUCKING){
            $tableuser = 'trucking';
        }else if($idrole==ROLE_INSW){
            $tableuser = 'insw';
        }
        return $tableuser;
    }
}
